package cn.itchao.exmaple.base;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

@Data
public class ApiResponse<T> implements Serializable {

    /**
     * 响应业务状态码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 是否有更多数据
     */
    private boolean more;


    public ApiResponse(Integer code,String message,T t){
        this.code = code;
        this.message = message;
        this.data = t;
    }

    public ApiResponse(){
        this.code = Status.SUCCESS.getCode();
        this.message = Status.SUCCESS.getMessage();
    }

    public static ApiResponse ofMessage(Integer code,String message){
        return new ApiResponse(code,message,null);
    }

    public static ApiResponse ofSuccess(Object t){
        return new ApiResponse(Status.SUCCESS.getCode(),Status.SUCCESS.getMessage(),t);
    }

    public static ApiResponse ofStatus(Status status){
        return new ApiResponse(status.getCode(),status.getMessage(),null);
    }

    public enum Status{
        SUCCESS(200,"ok"),
        BAD_REQUEST(400,"Bad Request"),
        NOT_FOUND(404,"Not Found"),
        INTERNAL_SERVER_ERROR(500,"Unknown Internal Error"),
        NOT_VALID_PARAM(40005,"Not valid Params"),
        NOT_SUPPORTED_OPERATION(40006,"Operation not supported");

        @Getter
        @Setter
        private Integer code;

        @Getter
        @Setter
        private String message;

        Status(Integer code, String message) {
            this.code = code;
            this.message = message;
        }

    }
    
}